package db;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

@SuppressWarnings("deprecation")
public class HibernateUtil {
    private static final SessionFactory sessionFactory;

    static {
        try {
            sessionFactory = new Configuration().configure().buildSessionFactory();
        } catch (Throwable e) {
            System.err.println("Error in creating SessionFactory object."
                    + e.getMessage());
            throw new ExceptionInInitializerError(e);
        }
    }

    /**
     * A static method for other application to get SessionFactory object
     * initialized in this helper class.
     */
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    
    @SuppressWarnings("unchecked")
    public static List<HooldusteenuseTellimus> getTellimused(String username){
	Session s = sessionFactory.openSession();
	s.beginTransaction();
	return s.createQuery("from HooldusteenuseTellimus h where kinnistu IN " +
			"(from Kinnistu where yyrnik=(from Yyrnik where kasutajanimi='"+username+"')) order by h.registrAeg desc ").list();
    }
    
    @SuppressWarnings("unchecked")
    public static List<Kinnistu> getKinnistud(String username){
	Session s = sessionFactory.openSession();
	s.beginTransaction();
	return s.createQuery("from Kinnistu where yyrnik = (from Yyrnik where kasutajanimi='"+username+"')").list();
    }
    
    @SuppressWarnings("unchecked")
    public static List<Hooldusteenus> getHooldusteenused(String type){
	Session s = sessionFactory.openSession();
	s.beginTransaction();
	return s.createQuery("from Hooldusteenus where hooldusteenuse_liik = (from HooldusteenuseLiik where nimetus='" + type + "')").list();
    }
    
    public static void insertHooldusteenuseTellimus(HooldusteenuseTellimus t){
	Session s = sessionFactory.openSession();
	s.beginTransaction();
	t.setHooldusteenuseTellimuseSeisundiLiik(
		(HooldusteenuseTellimuseSeisundiLiik) s.load(HooldusteenuseTellimuseSeisundiLiik.class, 0));
	t.setHooldusteenuseTellimuseKliendiOtsuseSeisundiLiik(
		(HooldusteenuseTellimuseKliendiOtsuseSeisundiLiik) s.load(HooldusteenuseTellimuseKliendiOtsuseSeisundiLiik.class, 0));
	s.save(t);
	s.getTransaction().commit();
    }
    
    @SuppressWarnings("unchecked")
    public static List<HooldusteenuseTellimus> getAllTellimused(){
	Session s = sessionFactory.openSession();
	s.beginTransaction();
	return s.createQuery("from HooldusteenuseTellimus").list();
    }
    
    public static HooldusteenuseTellimus getTellimusById(Integer id){
	Session s = sessionFactory.openSession();
	s.beginTransaction();
	return (HooldusteenuseTellimus) s.get(HooldusteenuseTellimus.class, id);
    }
    
    @SuppressWarnings("unchecked")
    public static List<HooldusteenuseTellimuseSeisundiLiik> getHoolduseenuseTellimuseLiikList(){
	Session s = sessionFactory.openSession();
	s.beginTransaction();
	return s.createQuery("from HooldusteenuseTellimuseSeisundiLiik").list();
    }
    
    public static void main(String[] args) {
	List<HooldusteenuseTellimuseSeisundiLiik> liigid = getHoolduseenuseTellimuseLiikList();
	for(HooldusteenuseTellimuseSeisundiLiik l : liigid){
	    System.out.println(l.getNimetus());
	} 
    }
    
}